package cn.simon.service;

import cn.simon.mapper.GoodsMapper;
import cn.simon.model.Goods;

import cn.simon.model.GoodsClassifySec;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Log
@Service
public class GoodsService {

    // 商品状态：下架
    private static final Integer STATE_ZERO = 0;

    // 商品状态：上架
    private static final Integer STATE_ONE = 1;

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 下架商品
     */
    public Integer freezeGoods(Integer id) {
        return goodsMapper.updateById(Goods.builder().goodsId(id).goodsState(0).build());
    }

    /**
     * 查询一级分类下所有商品
     *
     * @param goodsClassifySecList
     * @return goodsList
     * @author Kinfer
     * @date 2022/10/24
     */
    public List<Goods> getAllGoodsByClassify(List<GoodsClassifySec> goodsClassifySecList) {
        if (goodsClassifySecList.isEmpty()) {
            return null;
        }

        List<Integer> classifySecIdList = new ArrayList<>();
        for (GoodsClassifySec goodsClassifySec : goodsClassifySecList) {
            classifySecIdList.add(goodsClassifySec.getClassifySecId());
        }

        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.in("classify_sec_id", classifySecIdList);
        wrapper.eq("goods_state", STATE_ONE);

        return goodsMapper.selectList(wrapper);
    }

    /**
     * 查询二级分类下所有商品
     *
     * @param classifySecId
     * @return goodsList
     * @author Kinfer
     * @date 2022/10/24
     */
    public List<Goods> getAllGoodsByClassifySec(Integer classifySecId) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("classify_sec_id", classifySecId);
        wrapper.eq("goods_state", STATE_ONE);

        return goodsMapper.selectList(wrapper);
    }

    /**
     * 查询所有在货架上的商品
     *
     * @return goodsList
     * @author Kinfer
     * @date 2022/10/26
     */
    public List<Goods> getAllGoodsOnShelf() {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_state", STATE_ONE);

        return goodsMapper.selectList(wrapper);
    }

    /**
     * 查询与关键字匹配的所有在货架上的商品
     *
     * @param keyword
     * @return goodsList
     * @author Kinfer
     * @date 2022/10/27
     */
    public List<Goods> getAllGoodsByKeyword(String keyword) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.like("goods_name", keyword).or().like("goods_introduce", keyword);

        return goodsMapper.selectList(wrapper);
    }


    public List<Goods> findAll() {
        return goodsMapper.selectList(null);
    }

    public int addGoods(Goods goods) {
        return goodsMapper.insert(goods);
    }

    public int deleteByGoodsId(Integer id) {
        return goodsMapper.deleteById(id);
    }

    public List<Goods> findBySellerId(Integer id) {
        return goodsMapper.selectBySellerId(id);
    }

    public Goods findByGoodsId(Integer id) {
        return goodsMapper.selectById(id);
    }

    public Goods updateByGoodsId(Integer id) {
        return goodsMapper.updateByGoodsId(id);
    }

    public int updateGoods(Goods goods) {
        return goodsMapper.updateById(goods);
    }

}
